# fastapi_app/dependencies.py
import os
import django
from aioredis import Redis
from django.db import connections


def setup_django():
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "seckill_java_topy.settings")
    django.setup()

    # 配置数据库连接池
    for conn in connections.all():
        conn.settings_dict['CONN_MAX_AGE'] = 60  # 连接复用时间
        conn.settings_dict['OPTIONS'] = {
            'max_connections': 100,
            'stale_timeout': 300
        }


setup_django()

# fastapi_app/dependencies.py
from redis.asyncio import from_url
from functools import lru_cache

@lru_cache
def get_redis() -> Redis:
    return from_url("redis://192.168.141.15:6379/1", decode_responses=True)